package com.kamistoat.reactortest.web;

import com.kamistoat.reactortest.entity.Order;
import com.kamistoat.reactortest.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.List;

/**
 * 直接继承 HandlerFunction
 */
@Component
public class OrderHandlerFunction implements HandlerFunction {

    @Autowired
    OrderService orderService;

    /**
     * 由于要重载，所以方法名无法更改。该方法实现查询全部订单
     */
    @Override
    public Mono<ServerResponse> handle(ServerRequest serverRequest) {
        /**
         * 对 ServerRequest进行处理，获取请求内容
         */
        long id = Long.parseLong(serverRequest.pathVariable("id"));

        /**
         * 将返回内容写入到 ServerResponse中返回
         */
        return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON).body(orderService.deleteOrderById(id), Order.class);
    }
}
